<?php
/**
* Controls for the "scribble-privileges" UI widget.
*
* @package Scribble
*/
use Buan\Model;
use Buan\ModelCriteria;
use Buan\ModelManager;
class ScribbleAuthWidget {

	/**
	* Generate some privilege data for the specified resource which can be used to
	* populate the "scribble-privileges" UI widget.
	*
	* Place the string returned by this method (which is already JSON encoded) in
	* the "value" attribute of the UI widget's hidden field.
	*
	* @param string|ScribbleModel Resource
	* @param string Privileges from this scenario-tag(s) will be displayed
	* @return string
	*/
	static public function getResourcePrivileges($tgtResource, $scenarioTags) {
		$privilegeData = new StdClass();
		$privilegeData->privileges = ScribbleAuth::getDefinedPrivilegesByScenario($scenarioTags);
		$privilegeData->roles = array();
		$privilegeData->assignments = array();
		foreach(ScribbleAuth::getPrivilegeAssignments($tgtResource) as $pa) {
			$role = $pa->findRelatives('Role')->get(0);
			$privilegeData->roles[$role->id] = $role->getDbData();
			foreach($privilegeData->privileges as $p) {
				$privilegeData->assignments[$role->id][$p->id] = $pa->resolvePrivilege($p->id);
			}
		}
		return json_encode($privilegeData);
	}

	/**
	* Gathers the POST data generated by the ui widget. The widget inserts fields
	* that will arrive in $_POST['privileges'].
	*
	* The returned object contains two properties:
	* array remove = A list of RolePrivilegesModels that should be removed
	* array create = A list of RolePrivilegesModels that should be saved
	*
	* @param string|ScribbleModel Resource to which the gathered data is related
	* @param array Privilege data (from your POST variable)
	* @return StdClass
	*/
	public static function gatherPrivilegesFromPost($tgtResource, $privileges) {

		// Gather existing privileges
		$result = new StdClass();
		$c = new ModelCriteria();
		$c->addClause(ModelCriteria::EQUALS, 'resource_id', $tgtResource);
		$result->remove = ModelManager::select('RolePrivileges', $c)->asArray();

		// Create a list of privileges to create
		$result->create = array();
		foreach($privileges as $roleId=>$privs) {
			$rp = Model::create('RolePrivileges');
			$rp->role_id = $roleId;
			$rp->resource_id = $tgtResource;
			$aInherited = count($privs);
			foreach($privs as $privId=>$p) {
				if($p==='allow') {
					$rp->allow($privId);
				}
				else if($p==='deny') {
					$rp->deny($privId);
				}
				else {
					$aInherited--;
				}
			}

			// If all privileges are set to "inherit", the RolePrivileges record is
			// serving no purpose so we don't bother creating it
			if($aInherited>0) {
				$result->create[] = $rp;
			}
		}

		return $result;
	}

	/**
	* Submit the gathered data to persistent storage.
	*
	* The $gatheredData argument is the result of
	* ScribbleAuthWidget::gatherPrivilegesFromPost()
	*
	* @param string|ScribbleModel Resource to which the privilege relate
	* @param StdClass Data to save/remove
	* @return void
	*/
	public static function saveGatheredPrivileges($tgtResource, $gathered) {

		// Remove any old RolePrivileges and create any new ones
		foreach($gathered->remove as $p) {
			$p->getModelManager()->delete($p);
		}
		foreach($gathered->create as $p) {
			$p->resource_id = $tgtResource;
			$p->getModelManager()->save($p);
		}
	}
}
?>